package com.sanpu.kxe.business.trade.presenter;

import android.content.Intent;
import android.text.TextUtils;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.trade.bean.PayLimit;
import com.sanpu.kxe.business.trade.contract.GatheringContract;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.view.ChoosePayDetailsActivity;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.IntentTool;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;

import java.math.BigDecimal;

import static com.umeng.commonsdk.stateless.UMSLEnvelopeBuild.mContext;

/**
 * Description：收款
 * Created by 薛勇军 on 2019/2/27.
 */

public class GatheringPresenter implements GatheringContract.Presenter {

    private GatheringContract.View mView;

    private UserConfigOutput configOutput;

    public GatheringPresenter(GatheringContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void initData() {
        configOutput = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        if (configOutput != null) {
            mView.showMarqueeView(configOutput.getMarqueePayText());
        }
    }

    @Override
    public void onCompletePay(String currentType, String gatheringMoney) {
        final UserCenterOutput retDatBean = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (retDatBean == null) return;
        if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, retDatBean.getChkSts())) {
            String btnText = "", contentText = "";
            switch (retDatBean.getChkSts()) {
                case CommonConfig.AUTHEN_NOTSUB:
                    btnText = "去认证";
                    contentText = "收款前请先完成认证";
                    break;
                case CommonConfig.AUTHEN_AUDIT:
                    btnText = "确定";
                    contentText = "您的实名认证正在审核中，请稍后";
                    break;
                case CommonConfig.AUTHEN_NOPASS:
                    btnText = "重新认证";
                    contentText = "您的实名认证审核失败，请重新认证";
                    break;
                case CommonConfig.AUTHEN_NOCOMPLETION:
                    btnText = "去补全";
                    contentText = "需要补全资料才能开启收款";
                    break;
            }
            DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mView.getContext(), contentText, "取消", btnText,
                    new DialogViewListener() {
                        @Override
                        public void btnRightConfirm() {
                            if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, retDatBean.getChkSts())) {
                                openCertificationActivity();
                            }
                        }

                        @Override
                        public void btnLeftCancel() {

                        }
                    });
            return;
        }
        if (TextUtils.equals(currentType, TradeConfig.GATHERING_PAY)) {
            if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, retDatBean.getChkSts())) {
                String btnText = "", contentText = "";
                switch (retDatBean.getChkSts()) {
                    case CommonConfig.AUTHEN_AUDIT:
                        btnText = "确定";
                        contentText = "您的实名认证正在审核中，请稍后";
                        break;
                    case CommonConfig.AUTHEN_NOPASS:
                        btnText = "重新补全";
                        contentText = "您的补全资料审核失败，请重新补全审核通过后才能开启扫码收款";
                        break;
                    case CommonConfig.AUTHEN_NOCOMPLETION:
                        btnText = "补全资料";
                        contentText = "需要补全资料才能开启扫码收款";
                        break;
                }
                DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mContext, contentText, "取消", btnText,
                        new DialogViewListener() {
                            @Override
                            public void btnRightConfirm() {
                                if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, retDatBean.getChkSts())) {
                                    openCertificationActivity();
                                }
                            }

                            @Override
                            public void btnLeftCancel() {

                            }
                        });
                return;
            }
            if (TextUtils.equals("N", retDatBean.getProceedsSwitch())) {
                GlobalDialog dialog = new GlobalDialog(mView.getContext())
                        .content(retDatBean.getPersonAuthRmk())
                        .headerIcon(R.drawable.img_fail_result);
                dialog.show();
                return;
            }
//            if (!TextUtils.isEmpty(retDatBean.getAppScanCredit())
//                    && TextUtils.equals("Y", retDatBean.getAppScanCredit())) {
//                DialogUtil.getInstance().dialogExistTitleTwoBtnHaveCallBack(mView.getContext(), "温馨提示",
//                        "开通扫码收款,请先添加至少一张信用卡", "取消", "去添加",
//                        new DialogViewListener() {
//                            @Override
//                            public void btnRightConfirm() {
//                                IntentTool.startActivity(mView.getContext(),
//                                        new Intent(mView.getContext(), AddBankSetupActivity.class), true);
//                            }
//
//                            @Override
//                            public void btnLeftCancel() {
//
//                            }
//                        });
//                return;
//            }
        }
        PayLimit payLimit;
        String toastHint;
        if (TextUtils.equals(currentType, TradeConfig.GATHERING_QUICKPAY)) {
            payLimit = new PayLimit(configOutput.getAppFasterMinAmt(), configOutput.getAppFasterMaxAmt());
            toastHint = "个人收款";
        } else {
            payLimit = new PayLimit(configOutput.getAppScanMinAmt(), configOutput.getAppScanMaxAmt());
            toastHint = "扫码收款";
        }
        BigDecimal amt = new BigDecimal(gatheringMoney);
        if (!TextUtils.isEmpty(payLimit.getMin() + "") && amt.compareTo(payLimit.getMin()) == -1) {
            ToastUtil.showTextViewPrompt(toastHint + "最低为￥" + payLimit.getMin());
            return;
        }
        if (!TextUtils.isEmpty(payLimit.getMax() + "") && amt.compareTo(payLimit.getMax()) == 1) {
            ToastUtil.showTextViewPrompt(toastHint + "最高为￥" + payLimit.getMax() + "单笔");
            return;
        }
        Intent builder = new Intent(mView.getContext(), ChoosePayDetailsActivity.class);
        builder.putExtra(TradeConfig.INTENT_REQTYPE, currentType);
        builder.putExtra(TradeConfig.INTENT_REQAMOUNT, gatheringMoney);
        IntentTool.startActivity(mView.getContext(), builder);
    }

    /**
     * 去认证
     */
    private void openCertificationActivity() {
        IMainProvider mainProvider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (mainProvider != null) {
            mainProvider.openCertificationActivity(mView.getContext(), true);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mView != null) {
            mView = null;
        }
    }
}
